{extend name="common:base" /}
{block name="content"}
<div class="row">
<form id="export-form" method="post" action="{:url('backup',['act'=>'export'])}">
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th><input class="check-all" checked="chedked" type="checkbox" value=""></th>
                <th>表名</th>
                <th class="hidden-480">数据引擎</th>
                <th class="hidden-480">数据量</th>
                <th class="hidden-480">数据大小</th>
                <th class="hidden-480">创建时间</th>
                <th>备份状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {volist name="list" id="table"}
                <tr>
                    <td class="num">
                        <input class="tables" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
                    </td>
                    <td>{$table.name}</td>
                    <td class="hidden-480">{$table.engine}</td>
                    <td class="hidden-480">{$table.rows}</td>
                    <td class="hidden-480">{$table.data_length|format_bytes}</td>
                    <td class="hidden-480">{$table.create_time}</td>
                    <td class="msg" id="id{$i-1}">未备份</td>
                    <td class="action">
                        <a class="ajax-get no-refresh" href="{:url('Database/optimize?tables='.$table['name'])}">优化表</a>&nbsp;
                        <a class="ajax-get no-refresh" href="{:url('Database/repair?tables='.$table['name'])}">修复表</a>
                    </td>
                </tr>
            {/volist}
        </tbody>
    </table>
    </div>
    <div class="row">
        <a id="export" class="btn btn-info" href="javascript:;" autocomplete="off">立即备份</a>
        <a id="optimize" class="btn btn-info" href="{:url('optimize')}">优化表</a>
        <a id="repair" class="btn btn-info" href="{:url('repair')}">修复表</a>
        <a class="btn btn-info" href="{:url('recovery')}">数据还原</a>
    </div>
</form>
{/block}
{block name="js_file"}
<script src="__ADMIN__/js/bootbox.js"></script>
{/block}
{block name="javascript"}
<script type="text/javascript">
$(function(){
        //全选的实现
    $(".check-all").click(function(){
        $(".tables").prop("checked", this.checked);
    });
    $(".tables").click(function(){
        var option = $(".tables");
        option.each(function(i){
            if(!this.checked){
                $(".check-all").prop("checked", false);
                return false;
            }else{
                $(".check-all").prop("checked", true);
            }
        });
    });
    var $form = $("#export-form"), $export = $("#export"), tables, $optimize = $("#optimize"), $repair = $("#repair");

    $optimize.add($repair).click(function(){
        $.post(this.href, $form.serialize(), function(data){
            if(data.code){
                bootbox.dialog({
                    message: data.msg, 
                    buttons: {
                        "success" : {
                            "label" : "确定",
                            "className" : "btn-sm btn-primary"
                        }
                    }
                });
            } else {
                bootbox.dialog({
                    message: data.msg, 
                    buttons: {
                        "success" : {
                            "label" : "确定",
                            "className" : "btn-danger"
                        }
                    }
                });
            }
            setTimeout(function(){
                $('#top-alert').find('button').click();
                $(this).removeClass('disabled').prop('disabled',false);
            },1500);
        }, "json");
        return false;
    });

    $export.click(function(){
        $export.parent().children().addClass("disabled");
        $export.html("正在发送备份请求...");
        $.post(
            $form.attr("action"),
            $form.serialize(),
            function(data){
                if(data.code){
                    tables = data.tables;
                    $export.html(data.msg + "开始备份，请不要关闭本页面！");
                    backup(data.data.tab);
                    window.onbeforeunload = function(){ return "正在备份数据库，请不要关闭！" }
                } else {
                    bootbox.dialog({
                        message: data.msg, 
                        buttons: {
                            "success" : {
                                "label" : "确定",
                                "className" : "btn-danger"
                            }
                        }
                    });
                    $export.parent().children().removeClass("disabled");
                    $export.html("立即备份");
                    setTimeout(function(){
                        $('#top-alert').find('button').click();
                        //$(that).removeClass('disabled').prop('disabled',false);
                    },1500);
                }
            },
            "json"
        );
        return false;
    });

    function backup(tab, status){
        status && showmsg(tab.id, "开始备份...(0%)");
        $.get($form.attr("action"), tab, function(data){
            if(data.code){
                showmsg(tab.id, data.msg);

                if(!$.isPlainObject(data.data.tab)){
                    $export.parent().children().removeClass("disabled");
                    $export.html("备份完成，点击重新备份");
                    window.onbeforeunload = function(){ return null }
                    return;
                }
                backup(data.data.tab, tab.id != data.data.tab.id);
            } else {
                bootbox.dialog({
                    message: data.msg, 
                    buttons: {
                        "success" : {
                            "label" : "确定",
                            "className" : "btn-danger"
                        }
                    }
                });
                $export.parent().children().removeClass("disabled");
                $export.html("立即备份");
                setTimeout(function(){
                    $('#top-alert').find('button').click();
                    $(that).removeClass('disabled').prop('disabled',false);
                },1500);
            }
        }, "json");

    }

    function showmsg(id, msg){
        $("#id"+id).html(msg);
    }
});
</script>
{/block}